﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;
using DACNPM.MyUserControl.UCHienThi;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCSuaDDH : UserControl
    {
        String idnhanvien;
        DonDatHangBUO ddhBUO = new DonDatHangBUO();
        NhaCungCapBUO NCCBUO = new NhaCungCapBUO();
        NumberBUO numBUO = new NumberBUO();
        HangHoaBUO hhBUO = new HangHoaBUO();
        int id;
        String idDDH, idHang;
        String idNCC;
        public UCSuaDDH(String id, String idtoancuc, String idNCC)
        {
            
            InitializeComponent();
            this.idNCC = idNCC;
            this.idnhanvien = idtoancuc;
            
            tbl_DonDatHang ddh = ddhBUO.GetSingle(id);
            lblIDNV.Text = id;
            this.idDDH = id;

            txtNCC.Text = ddhBUO.GetIDNCCByTenNCC(idNCC);
            dateTimePicker1.Text = ddh.NgayDat.ToString();
            dateTimePicker2.Text = ddh.NgayGiao.ToString();

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachThemDDH(idDDH, txtNCC.Text);
            showComboboxHang(); 
            binding();
        }

        /// <summary>
        /// binding từ datagridview lên textbox
        /// </summary>
        void binding()
        {
            cbxTenHang.DataBindings.Clear();
            cbxTenHang.DataBindings.Add("Text", dataGridView1.DataSource, "TenHang");
            //txtSoLuong.DataBindings.Clear();
            //txtSoLuong.DataBindings.Add("Text", dataGridView1.DataSource, "SoLuong");
            lblIDHang.DataBindings.Clear();
            lblIDHang.DataBindings.Add("Text", dataGridView1.DataSource, "id_Hang");
        }

        /// <summary>
        /// hiển thị combobox hàng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void showComboboxHang()
        {
            cbxTenHang.DataSource = ddhBUO.getHangByNCC(this.idNCC);
            cbxTenHang.DisplayMember = "TenHang";
            cbxTenHang.ValueMember = "id_Hang";
        }

        private void rdTimMaDDH_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePicker3.Enabled = false;
            dateTimePicker4.Enabled = false;
            txtTimTheoMa.Text = "";
            txtTimTheoMa.Enabled = true;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachThemDDH(idDDH, txtNCC.Text);
            dataGridView1.Enabled = true;
            binding();
        }

        private void rdTimNgayDH_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePicker3.Enabled = true;
            dateTimePicker4.Enabled = false;
            txtTimTheoMa.Text = "";
            txtTimTheoMa.Enabled = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachThemDDH(idDDH, txtNCC.Text);
            dataGridView1.Enabled = true;
            binding();
        }

        private void rdTimNgayGH_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePicker3.Enabled = false;
            dateTimePicker4.Enabled = true;
            txtTimTheoMa.Text = "";
            txtTimTheoMa.Enabled = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachThemDDH(idDDH, txtNCC.Text);
            dataGridView1.Enabled = true;
            binding();
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtSoLuong.Text = "";
            cbxTenHang.Text = "";
        }

        private void rdXemTatCa_CheckedChanged(object sender, EventArgs e)
        {
            dateTimePicker3.Enabled = false;
            dateTimePicker4.Enabled = false;
            txtTimTheoMa.Text = "";
            txtTimTheoMa.Enabled = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = ddhBUO.HienThiDanhSachThemDDH(idDDH, txtNCC.Text);
            dataGridView1.Enabled = true;
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        idDDH = dataGridView1.Rows[id].Cells[0].Value.ToString();
                        idHang = dataGridView1.Rows[id].Cells[1].Value.ToString();
                        
                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void xemDanhSáchĐơnĐặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachDDH ucDSDDH = new UCDanhSachDDH(idnhanvien);
            Form form = this.ParentForm;

            ControlAccess.ShowConTrol(form, ucDSDDH, "grbHienThiChucNang");
        }

        private void thêmĐơnĐặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemDDH ucThemDDH = new UCThemDDH(idnhanvien);
            Form form = this.ParentForm;

            ControlAccess.ShowConTrol(form, ucThemDDH, "grbHienThiChucNang");
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if ((cbxTenHang.Text == "") || (txtSoLuong.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else 
            {
                if (numBUO.isNumberInput(txtSoLuong.Text) == false)
                {
                    MessageBox.Show("Trường số lượng phải nhập số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    tbl_DonDatHang ddh = new tbl_DonDatHang();
                    ddh.NgayGiao = DateTime.Parse(dateTimePicker2.Text);
                    tbl_DDH_Hang ddhh = new tbl_DDH_Hang();
                    ddhh.SoLuong = int.Parse(txtSoLuong.Text);
                    //String idHang = hhBUO.GetIDHangByTenHang(cbxTenHang.Text);

                    // quan trong


                    if (ddhBUO.Update(this.idDDH, ddh) && ddhBUO.UpdateDDH_Hang(this.idDDH, lblIDHang.Text, ddhh))
                    {
                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = ddhBUO.HienThiDanhSachThemDDH(idDDH, txtNCC.Text);
                        dataGridView1.Enabled = true;
                        MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Cập nhật không thành công. Mặt hàng này chưa có trong đơn đặt hàng.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            //binding();
        }

        private void txtTimTheoMa_TextChanged(object sender, EventArgs e)
        {
            var list = ddhBUO.searchByMaDDH(txtTimTheoMa.Text);   //Chua xong
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = list;
            dataGridView1.Visible = true;
        }

        private void cbxTenHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblIDHang.Text = cbxTenHang.SelectedValue.ToString();
        }

        private void rdCapNhat_CheckedChanged(object sender, EventArgs e)
        {
            btnCapNhat.Enabled = true;
            btnThem.Enabled = false;
            dateTimePicker2.Enabled = true;
        }

        private void rdThem_CheckedChanged(object sender, EventArgs e)
        {
            btnCapNhat.Enabled = false;
            btnThem.Enabled = true;

            dateTimePicker2.Text = ddhBUO.GetSingle(this.idDDH).NgayGiao.ToString();
            dateTimePicker2.Enabled = false;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if ((cbxTenHang.Text == "") || (txtSoLuong.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (numBUO.isNumberInput(txtSoLuong.Text) == false)
                {
                    MessageBox.Show("Trường số lượng phải nhập số.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    tbl_DDH_Hang ddhh = new tbl_DDH_Hang();
                    ddhh.id_DonDatHang = this.idDDH;
                    ddhh.id_Hang = lblIDHang.Text;
                    ddhh.SoLuong = int.Parse(txtSoLuong.Text);

                    if (ddhBUO.InsertDDH_Hang(ddhh))
                    {
                        dataGridView1.DataSource = ddhBUO.HienThiDanhSachThemDDH(idDDH, txtNCC.Text);
                        MessageBox.Show("Thêm đơn đặt hàng thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Mặt hàng này đã được thêm vào đơn đặt hàng.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            binding();
        }

        private void xóaĐơnĐặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn là muốn xóa hay không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (ddhBUO.DeleteChiTietDDH(this.idDDH, this.idHang))
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ddhBUO.HienThiDanhSachThemDDH(this.idDDH, txtNCC.Text);
                    txtNCC.Text = "";
                    lblIDHang.Text = "";
                    lblIDNV.Text = "";
                    txtSoLuong.Text = "";
                    cbxTenHang.Text = "";
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
    }
}
